package com.shengde.jinwawaforleader.model.homePage;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.network.protocolbean.ProSchoolMiens;
import com.shengde.jinwawaforleader.views.SchoolMienPopupWindow;
import com.shengde.jinwawaforleader.views.SelectPicPopupWindow;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * @author 鞠永强
 * @version $Rev$
 * @time 2017/2/15 0015 17:41
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes ${TODO}
 */

public class EditNewThreeLinkActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    private TextView tv_back;
    private TextView tv_title;
    private TextView fill_add;
    private RelativeLayout mThreeUrl;
    private RelativeLayout relative_cover_picture;
    private RelativeLayout relative_share_title;
    private RelativeLayout relative_kingarden_share_content;
    private CheckBox check_box;
    private ProSchoolMiens.SitelistEntity sitelistEntities;
    private boolean isChecked;
    private SelectPicPopupWindow showPopupWindow; //自定义popuowindow
    private String picName;  //拍摄图片的名字
    private static final int TAKE_PICTURE = 1;
    private static final int SELECT_PICTURE = 2;
    private ImageView iv_add_pic;
    private String path = "";
    private SchoolMienPopupWindow schoolMienPopupWindow;

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_new_edit_three_link);
    }

    @Override
    public void initView() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        fill_add = (TextView) findViewById(R.id.fill_add);
        mThreeUrl = (RelativeLayout) findViewById(R.id.relative_kingarden_name);
        relative_cover_picture = (RelativeLayout) findViewById(R.id.relative_cover_picture);
        relative_share_title = (RelativeLayout) findViewById(R.id.relative_share_title);
        relative_kingarden_share_content = (RelativeLayout) findViewById(R.id.relative_kingarden_share_content);
        check_box = (CheckBox) findViewById(R.id.check_box);
        iv_add_pic = (ImageView) findViewById(R.id.iv_cover_drawable);
    }

    @Override
    public void initData() {
        tv_title.setText(getString(R.string.edit_three_link));
        fill_add.setText(getString(R.string.more_));
        sitelistEntities = (ProSchoolMiens.SitelistEntity) getIntent().getSerializableExtra("sitelistEntities");

        if (sitelistEntities != null) {

            if ("1".equals(sitelistEntities.type)) {
                check_box.setChecked(true);
                isChecked = true;
            }
        } else {
            check_box.setChecked(false);
        }
    }

    @Override
    public void initListener() {
        mThreeUrl.setOnClickListener(this);
        relative_cover_picture.setOnClickListener(this);
        relative_share_title.setOnClickListener(this);
        relative_kingarden_share_content.setOnClickListener(this);
        tv_back.setOnClickListener(this);
        fill_add.setOnClickListener(this);
        check_box.setOnCheckedChangeListener(this);
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.relative_kingarden_name:
                Intent intent = new Intent(this, EditDetailActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.relative_cover_picture:
                showPopupWindow(view);
                break;
            case R.id.relative_share_title:
                intent = new Intent(this, EditDetailActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            case R.id.relative_kingarden_share_content:
                intent = new Intent(this, EditDetailActivity.class);
                intent.putExtra("type", 3);
                startActivity(intent);
                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.fill_add:
                showPopBottom(view);
                break;
        }
    }

    /**
     * 当从"编辑第三方"调转过来的时候 点击更多的弹窗
     *
     * @param view
     */
    private void showPopBottom(View view) {
        schoolMienPopupWindow = new SchoolMienPopupWindow(this, itemOnClick);
        schoolMienPopupWindow.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 3);
        schoolMienPopupWindow.relative_share.setVisibility(View.GONE);
        schoolMienPopupWindow.tv_editor_custom.setText(getString(R.string.save));
        schoolMienPopupWindow.iv_editor_custom.setImageResource(R.drawable.pop_preview);
        schoolMienPopupWindow.tv_editor_link.setText(getString(R.string.preview));
        schoolMienPopupWindow.iv_editor_link.setImageResource(R.drawable.pop_save);
        //设置layout在PopupWindow中显示的位置
        schoolMienPopupWindow.showPopupWindow(view);
    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            schoolMienPopupWindow.dismiss();
            switch (v.getId()) {
                case R.id.relative_editor_link:
                    Intent intent = new Intent(EditNewThreeLinkActivity.this, PreviewSchoolMien.class);
//                    intent.putExtra("url", edt_three_link_contact.getText().toString().trim());
                    startActivity(intent);
                    break;
                case R.id.relative_editor_custom:
                    if (isChecked) {
                     /*   mType = "1";
                        if (path == null || "".equals(path)) {
                            showSaveUrl(null, mType);
                        } else {
                            compressImage();
                        }*/
                    } else {
                     /*   mType = "2";
                        if (path == null || "".equals(path)) {
                            showSaveUrl(null, mType);
                        } else {
                            compressImage();
                        }
*/
                    }

                    break;
            }
        }
    };

    /**
     * 显示 图片弹窗
     *
     * @param v
     */
    private void showPopupWindow(View v) {

        showPopupWindow = new SelectPicPopupWindow(this, itemsOnClick);
        showPopupWindow.tv_video_babydiary_share.setText(getString(R.string.take_photo));
        showPopupWindow.tv_video_babydiary_share.setTextColor(Color.parseColor("#067DFF"));
        showPopupWindow.tv_video_babydiary_delete.setTextColor(Color.parseColor("#067DFF"));
        showPopupWindow.tv_look_big_pic.setVisibility(View.GONE);
        showPopupWindow.tv_video_babydiary_delete.setText(getString(R.string.get_phone_picture));
        showPopupWindow.tv_video_babydiary_cancle.setTextColor(Color.parseColor("#067DFF"));

        //设置layout在PopupWindow中显示的位置
        showPopupWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_video_babydiary_share:  //拍摄
                    //先验证手机是否有sdcard
                    String status = Environment.getExternalStorageState();
                    if (status.equals(Environment.MEDIA_MOUNTED)) {
                        try {
                            File dir = new File(Environment.getExternalStorageDirectory() + "/sdcard/cache/");
                            if (!dir.exists()) {
                                dir.mkdirs();
                            }
                            Intent intent = new Intent();
                            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                            picName = new Date().getTime() + ".jpg";
                            Uri uri = Uri.fromFile(new File(dir, picName));
                            intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                            startActivityForResult(intent, TAKE_PICTURE);
                        } catch (ActivityNotFoundException e) {
                            Toast.makeText(EditNewThreeLinkActivity.this, "没有找到储存目录", Toast.LENGTH_LONG).show();
                        }
                    }
                    showPopupWindow.dismiss();

                    break;
                case R.id.tv_video_babydiary_delete: //手机中获取
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_PICK);
                    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(intent, SELECT_PICTURE);
                    showPopupWindow.dismiss();
                    break;
                case R.id.tv_video_babydiary_cancle:
                    showPopupWindow.dismiss();
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_PICTURE) {
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    Uri uriPath = data.getData();

                    if (!"".equals(uriPath.getAuthority())) {
                        Cursor cursor = getContentResolver().query(uriPath,
                                new String[]{MediaStore.Images.Media.DATA},
                                null, null, null);
                        cursor.moveToFirst();
                        path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    } else {
                        path = uriPath.getPath();
                    }
                    Glide.with(this).load(path).into(iv_add_pic);

                }
            }
        } else if (requestCode == TAKE_PICTURE) {
            if (resultCode == RESULT_OK) {

                String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sdcard/cache/" + picName;

                Bitmap bm = BitmapFactory.decodeFile(filePath);

                final File myCaptureFile = new File(filePath);
                try {
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(myCaptureFile));
                    /* 采用压缩转档方法 */
                    bm.compress(Bitmap.CompressFormat.JPEG, 60, bos);
                    /* 调用flush()方法，更新BufferStream */
                    bos.flush();
                    /* 结束OutputStream */
                    bos.close();
                    path = myCaptureFile.getAbsolutePath();
                    Glide.with(this).load(path).into(iv_add_pic);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }
}
